package mathUtils;

import static java.lang.Math.*;

public class VecMath {
	
	public static float[] vecByMagAngle(float r, float ang) {
		float[] a = vecUnitByAngle(ang);
		vecScale(a, r);
		return a;
	}
	
	public static void vecZero(float[] a) {
		for (int i=0;i<a.length;++i) a[i] = 0f;
	}
	public static void vecZero(float[][] a) {
		for (int i=0;i<a.length;++i)
			if (a[i] != null)
				for (int j=0; j<a[i].length;++j)
					a[i][j] = 0f;
	}
	
	public static void vecCopy(float[] a, float[] b) {
		for (int i=0;i<b.length;++i) b[i] = a[i];
	}
	public static void vecCopy(float[][] a, float[][] b) {
		for (int i=0;i<b.length;++i)
			if (b[i] != null)
				vecCopy(a[i], b[i]);
	}

	public static float[] vecCloneSpace(float[] a) {
		float[] b = new float[a.length];
		return b;		
	}
	public static float[][] vecCloneSpace(float[][] a) {
		float[][] b = new float[a.length][];
		for (int i=0;i<a.length;++i)
			if (a[i] != null)
				b[i] = vecCloneSpace(a[i]);
		return b;
	}
	
	public static float vecSum(float a[]) {
		float s = 0;
		for (float e : a) s += e;
		return s;
	}
	public static void vecSum(float a[][], float s[]) {
		for (int i = 0; i< s.length; ++i)
			if (a[i]!=null) s[i] = vecSum(a[i]);
	}
	public static float[] vecSum(float a[][]) {
		float s[] = new float[a.length];
		vecSum(a, s);
		return s;
	}
	
	public static void vecAdd(float[] a, float[] b, float[] c) {
		for (int i=0;i<c.length;++i) c[i] = a[i] + b[i];
	}
	public static void vecAdd(float[][] a, float[][] b, float[][] c) {
		for (int i=0;i<c.length;++i)
			if (c[i] != null)
				for (int j=0;j<c[i].length;++j) 
					c[i][j] = a[i][j] + b[i][j];
	}
	public static void vecIncr(float[] a, float[] b) {vecAdd(a, b, a);}
	
	public static void vecSub(float[] a, float[] b, float[] c) {
		for (int i=0;i<c.length;++i) c[i] = a[i] - b[i];
	}
	public static void vecDecr(float[] a, float[] b) {vecSub(a, b, a);}

	public static void vecMult(float[] a, float b, float[] c) {
		for (int i=0;i<c.length;++i) c[i] = a[i]*b;
	}
	public static void vecScale(float[]a, float b) {vecMult(a, b, a);}
	public static void vecScale(float[][] a, float[] b) {
		for (int i=0;i<a.length;++i)
			if (a[i]!=null) vecScale(a[i], b[i]);
	}
	
	public static void vecDiv(float[] a, float b, float[] c) {
		for (int i=0;i<c.length;++i) c[i] = a[i]/b;
	}
	public static void vecScaleDown(float[]a, float b) {vecDiv(a, b, a);}
	public static void vecScaleDown(float[][] a, float[] b) {
		for (int i=0;i<a.length;++i)
			if (a[i]!=null) vecScaleDown(a[i], b[i]);
	}
	
	public static void vecAddScaled(float[] a, float[] b, float m, float[] c) {
		for (int i=0;i<c.length;++i) c[i] = a[i] + m*b[i];
	}
	public static void vecAddScaled(float[][] a, float[][] b, float m, float[][] c) {
		for (int i=0;i<c.length;++i)
			if (c[i] != null)
				vecAddScaled(a[i], b[i], m, c[i]);
	}
	public static void vecAddScaled(float[] a, float[][] b, float[] m, float[]c) {
		for (int i=0;i<m.length;++i)
			vecAddScaled(a, b[i], m[i], c);
	}
	public static void vecAddScaled(float[][] a, float[][][] b, float[] m, float[][] c) {
		for (int i=0;i<m.length;++i)
			vecAddScaled(a, b[i], m[i], c);
	}

	public static float vecDot(float[] a, float[] b) {
		float r = 0;
		for (int i=0;i<a.length;++i) r += a[i] * b[i];
		return r;
	}
	public static float vec2Cross(float[] a, float[] b) {
		return (a[0]*b[1] - a[1]*b[0]);
	}

	public static float vecMag2(float[]a) {
		float r = 0f;
		for (float e : a) r += e*e;
		return r;
	}
	public static float vecMag(float []a) {
		return (float) sqrt(vecMag2(a));
	}
	public static float vecDist(float[]a, float[] b) {
		float c[] = new float[a.length];
		vecSub(a, b, c);
		return vecMag(c);
	}
	public static float vecDist2(float[]a, float[] b) {
		float c[] = new float[a.length];
		vecSub(a, b, c);
		return vecMag2(c);
	}
	public static void vecUnitByAngle(float ang, float[] a) {
		a[0] = (float) cos(ang);
		a[1] = (float) sin(ang);
	}
	public static float[] vecUnitByAngle(float ang) {
		float a[] = new float[2];
		vecUnitByAngle(ang, a);
		return a;
	}
}
